<?php
//-------------------------------------------------------------------------------------------------------
    require_once($config['app_dir'] . '/includes/object.class.php');
    require_once($config['app_dir'] . '/includes/db.class.php');
    //---------------------------------------------------------------------------------------------------
    class objUserParams extends objObject implements iErrorHandler, iDebug{
            public $id = false;

            protected $_error_str = array('no error',
                                         'Not set user id.'
                                        );

        function __construct() {
            $this->_getID();
        } //constructor

        public function ClassName() {
            return  __CLASS__;
        }//ClassName

        public function dump() {
            echo"<br>\n Dump of ".$this->ClassName().": " ;
            echo"<br>\n id= "; var_dump($this->ClassName());
        } // dump

        //----------------------------------------------------------------------
        protected function _getID() {
            $this->id = intval($_GET['user_id']);
            if ( !isset($_GET['user_id']) || !is_numeric($_GET['user_id']) || empty($_GET['user_id']) ) {
                throw new Exception($this->Error(1));
                return false;
            }
            return $this->id;
        } //getTrand

    } //class objUserParams
    //---------------------------------------------------------------------------------------------------
    class objUserModel {
            public $id = false;
            protected $_db = false;
            protected $_record = false;
            protected $_sql_querys = array();

        public function __construct($aID=false) {
            $this->id = $aID;

            $this->_db = objDataBase::GetInstance();
            $this->_sqls();

            try {
                ADOdb_Active_Record::SetDatabaseAdapter($this->_db->handler);
                $this->_record = new ADOdb_Active_Record('fa_users');
            } catch(exceptions $e) {
                if (true == $this->_DataBase->handler->debug ) {
                    adodb_pr($e);
                    $e = adodb_backtrace($e->trace);
                }
                echo 'Error: '.$e->sql.' '.$e->getMessage();
            }
        } //constructor

        public function ActiveRecord() {
            return $this->_record;
        } //activeRecord

        public function GetTrades($user_id=false) {
            if( $user_id ) $this->id = $user_id;
            return $this->_db->handler->getAll($this->_sql_querys['trades'], intval($user_id));
            /*
            $data = $this->_db->handler->getAll($this->_sql_querys['trades'], intval($user_id));
            $trades = array();
            foreach($data as $item)      $trades[] = intval($item['TradeID']);
            return $trades;
            */
        } //GetTrades

        public function GetTradeSystemArr($user_id=false, $system_id=false) {
            if( $user_id ) $this->id = $user_id;
            if( !$this->id ) return false;
            if( $system_id )
                $data = $this->_db->handler->getAll($this->_sql_querys['trades_system'], array(intval($this->id), intval($system_id)));
            else
                $data = $this->_db->handler->getAll($this->_sql_querys['trades'], array(intval($this->id)));

            return $data;
        } //GetTradeSystemArr

        //-----------------------------------------------------------------------------------------------
        protected function _sqls() {
            $this->_sql_querys['trades'] = //" SELECT ut.TradeID , ut.SystemID  FROM fa_users_trades ut  WHERE ut.UserID=? ";
                " SELECT ut.TradeID , ut.SystemID " .
                " FROM fa_users_trades ut ".
                    " LEFT OUTER JOIN fa_rates r ON(r.TradeID = ut.TradeID) " .
                " WHERE ut.UserID=? " ;
            //------------------------------------------------------------------------------------------
            $this->_sql_querys['trades_system'] = $this->_sql_querys['trades'] . ' AND  SystemID=? ';
            //------------------------------------------------------------------------------------------
            $this->_sql_querys['trades'] .= " ORDER BY r.SortIndex ";
            $this->_sql_querys['trades_system'] .= " ORDER BY r.SortIndex ";
        }//_sqls

    }//class UserModel
    //---------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------
?>
